package com.hmall.api.interceptor;

import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Author：cxl
 * @Package：com.hmall.api.interceptor
 * @Project：hmall-micro1
 * @name：FeignInterceptor
 * @Date：2025/11/13 12:19
 * @Filename：FeignInterceptor
 */
@Component
@Slf4j
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //远程调用时，服务提供者如何获取登录用户id
        //feigin远程调用时
        Long loginUserId = UserContext.getUser();
        log.info("loginUserId{}",loginUserId);
        if (loginUserId != null) {
            //如果当前线程上有，就将用户id添加给Feign请求的请求头user-info
            requestTemplate.header("user-info", UserContext.getUser().toString());
        }
    }
}
